package com.zs4sdk.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

import com.zs4sdk.common.Extra;
import com.zs4sdk.extra.Zs4GameCode;
import com.common.library.listener.OnProcessListener;

/**
 * 
 * function: 广播处理接收: 接收时能unregisterReceiver（用这种要注意分支都有发广播不然会泄漏）
 * 
 * @ author: aXiBa
 */
public class SDKGameProcessReceiverForAutoUnRegister extends BroadcastReceiver {
	
	private static final String TAG = SDKGameProcessReceiverForAutoUnRegister.class.getSimpleName();
	
	String action;   // 监听代码
	
	OnProcessListener mOnProcessListener;    // 监听处理回调 
	
	Context ctx;
	
	public SDKGameProcessReceiverForAutoUnRegister(String action, OnProcessListener mOnProcessListener, Context ctx) {
		this.action = action;
		this.ctx = ctx;
		this.mOnProcessListener = mOnProcessListener;
	}

	@Override
	public void onReceive(Context arg0, Intent intent) {
		String curAction = intent.getAction();
		// 登录
		if(curAction.equals(action)) {
//			LogUtil.i(TAG, "接收到。。。。。action： " + curAction);
			
			// 回调完成, 去掉receiver
			ctx.unregisterReceiver(this);
//			LogUtil.i(TAG, "去掉Receiver。。。。。");
			
			
			if(null != mOnProcessListener) {
//				Log.d(TAG, "onReceive action" + curAction + "   code: " + intent.getIntExtra(Extra.INTENTKEY.CODE, Zs4GameCode.CANCEL));
				mOnProcessListener.finishProcess(intent.getIntExtra(Extra.INTENTKEY.CODE, Zs4GameCode.BACKTOGAME), intent.getExtras());
			}
		}
	}

}
